import { elkAxiosInstance } from ".."

// TODO 添加排序、类型等
export const goodsSearch = (str: string|null, page: number) => {
  if (str == null) {
    return elkAxiosInstance({
      url: "/es/goods/_search",
      method: "post",
      data: {
        size: 2000
      }
    })
  }
  return elkAxiosInstance({
    url: "/es/goods/_search",
    method: "post",
    data: {
      query: {
        bool: {
          should: [
            {
              match: {
                name: str
              }
            },
            {
              match: {
                des: str
              }
            }
          ]
        }
      },
      size: 2000
    }
  })
}

export const dealsCurrent = (goodId: number) => {
  return elkAxiosInstance({
    url: "/es/deals/_search",
    method: "post",
    data: {
      query: {
        bool: {
          must: [
            {
              term: {
                state: "finish"
              }
            },
            {
              term: {
                good_id: goodId
              }
            }
          ]
        }
      },
      _source: ["deal_cost", "num", "updated"],
      size: 6
    }
  })
}
export const salesSToB = (goodId: number) => {
  return elkAxiosInstance({
    url: "/es/sales/_search",
    method: "post",
    data: {
      query: {
        bool: {
          must: [
            {
              term: {
                state: "ready"
              }
            },
            {
              term: {
                good_id: goodId
              }
            }
          ]
        }
      },
      sort: ["min_price"],
      _source: ["min_price", "left_num", "updated"],
      size: 6
    }
  })
}
export const buysBToS = (goodId: number) => {
  return elkAxiosInstance({
    url: "/es/buys/_search",
    method: "post",
    data: {
      query: {
        bool: {
          must: [
            {
              term: {
                state: "ready"
              }
            },
            {
              term: {
                good_id: goodId
              }
            }
          ]
        }
      },
      sort: [
        {
          max_price: {
            order: "desc"
          }
        }
      ],
      _source: ["max_price", "left_num", "updated"],
      size: 6
    }
  })
}

